<?php
class UpdateAtion extends CAction
{
    public function run()
    {
        $module = $this->controller->get('m', $this->controller->taxModule);
        if ($module=='')
            $module = $this->controller->taxModule;
        $voc = $this->controller->getVocabulary();
        if (is_object($voc))
        {
            $v = $voc->id;
            $vob = $voc;
        }
        else
        {
            $v = $this->controller->get('v', 0);        
            $vob = Vocabulary::model()->findByPk($v);
            if (!is_object($vob))
                throw new CHttpException(400,Yii::t('Core','Invalid Type ID.'));
        }
        $this->controller->voc = $v;
            
        $propertyClassName = $vob->propertyClassName;
        $propertyModule = $vob->propertyModule;
        $property = '';
        
        if (Yii::app()->request->IsPostRequest) {
            // save posted data
            $this->saveData($module, $v, $propertyModule, $propertyClassName);
        } else {
            // show edit form
            $id = $this->controller->get('id', 0);
            $model = $this->getTerm($id, $v);
            $property = $this->getProperty($id, $propertyClassName);
        }
        
        $this->controller->render('update', array(
            'model' => $model,
            'v'=>$v,
            'module'=>$module,
            'property'=>$property,
            'propertyModule'=>$propertyModule,
            'propertyClassName'=>$propertyClassName,
        ));
    }
    
    protected function saveData($module, $v, $propertyModule, $propertyClassName)
    {
        // check it is action create or update
        if (isset($_POST['Term']['id']) && !empty($_POST['Term']['id']))
            $action = 'update';
        else
            $action = 'create';
        
        $_POST['validateOnly'] = ($this->controller->post('ajax','') == 'term-form');
        $result = FSM::run('Core.Term.save', $_POST);
        $model = $result->model;

        if ($this->controller->post('ajax','') == 'term-form'){
            echo $result->getActiveErrorMessages($result->model);
            Yii::app()->end();
        }   
        if (! $result->hasErrors())
        {
            if (empty($propertyClassName) === false)
            {
                $_POST[$propertyClassName]['term_id'] = $model->id;
                FSM::run($propertyModule.'.'.$propertyClassName.'.save', $_POST);
            }
            $this->controller->message = Yii::t('Core','Item has been saved successfully.');
            if ($action == 'update')
                $this->controller->redirect($this->controller->createUrl('update',array('id' => $model->id,'v'=>$v,'m'=>$module)));
            else
            {
                Yii::app()->user->setFlash('category_just_added',$model->id);
                $this->controller->redirect($this->controller->createUrl('create',array('v'=>$v,'m'=>$module)));    
            }
        }
    }
    
    /**
    * get Term by id
    * 
    * @param int $id
    * @param int $v vocabulary ID
    * 
    * @return Term
    */
    protected function getTerm($id, $v)
    {
        if ($id == 0) {
            $model = new Term();
            $model->v_id = $v;
        } else {
            $model = FSM::run('Core.Term.get', array('id' => $id))->model;
        }
        return $model;
    }
    
    /**
    * get property of Term
    * 
    * @param int $id term ID
    * @param string $propertyClassName Class Name of property class
    * 
    * @return Object|string
    */
    protected function getProperty($id, $propertyClassName)
    {
        $property = '';
        if (empty($propertyClassName) === false)
        {
            if ($id)
                $property = Term::model($propertyClassName)->findByAttributes(array('term_id'=>$id));
            if (!isset($property) || !is_object($property))
            {
                $property = new $propertyClassName;
            }
            if (isset($property->xml))
                $property->xml = base64_decode($property->xml);
        }
        return $property;
    }
}
